<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 11:28
 */

namespace app\api\model;

use app\api\model\Order as OrderModel;
use app\api\model\MemberFigureMeasureData as MemberFigureMeasureDataModel;
use app\api\model\PatternMaster as PatternMasterModel;
use app\api\service\Token;
use app\lib\exception\OrderException;
use app\lib\exception\PatternMasterException;
use think\Db;
use think\Exception;

class OrderFigureMeasureData extends BaseModel {

    protected $hidden = ['update_time','delete_time', 'create_time'];

    public function getImage_1Attr($value) {
        return get_img_url($value);
    }

    public function getImage_2Attr($value) {
        return get_img_url($value);
    }

    public function getImage_3Attr($value) {
        return get_img_url($value);
    }

    /**
     * 根据订单ID获取量体数据，支持量体师获取
     * @param $id
     * @param $is_pattern_master
     * @return null|\think\Model
     * @throws OrderException
     * @throws PatternMasterException
     */
    public static function getFigureMeasureByOrderId($id, $is_pattern_master) {
        $uid = Token::getCurrentUid();

        if ($is_pattern_master) {
            $patternMaster = PatternMasterModel::get(['member_id' => $uid, 'status' => 2]); // 获取量体师信息
            if (!$patternMaster) {
                throw new PatternMasterException([
                    'msg' => '您还没有申请量体师，或者申请还没有通过',
                    'errorCode' => 13001
                ]);
            }
            $where = ['id' => $id, 'pattern_master_id' => $patternMaster['id']];
        } else {
            $where = ['id' => $id, 'member_id' => $uid];
        }

        $order = OrderModel::get($where);
        if (!$order) {
            throw new OrderException();
        }
        $data = self::get(['order_id' => $id]);
        return $data;
    }

    /**
     * 创建或更新订单量体数据
     * @param $params
     * @return bool
     * @throws Exception
     * @throws OrderException
     */
    public static function createOrUpdateFigureMeasure($params) {

        $uid = Token::getCurrentUid();  // 本接口由量体师调用  所以token为量体师所有

        $patternMaster = PatternMasterModel::get(['member_id' => $uid, 'status' => 2]); // 获取量体师信息
        if (!$patternMaster) {
            throw new PatternMasterException([
                'msg' => '您还没有申请量体师，或者申请还没有通过',
                'errorCode' => 13001
            ]);
        }

        $order = OrderModel::get(['id' => $params['order_id'], 'pattern_master_id' => $patternMaster['id']]);
        if (!$order) {
            throw new OrderException();
        }
        $figure_measure = self::get(['order_id' => $params['order_id']]);
        $params['sex'] = $order->orderProduct->product->type;

        Db::startTrans();
        try {
            if ($figure_measure) {
                $figure_measure->save($params);
            } else {
                $params['member_id'] = $order['member_id'];
                $figure_measure->create($params);
            }

            // 更新用户量体数据
            unset($params['order_id']);
            $member_figure_measure_data = MemberFigureMeasureDataModel::get(['member_id' => $order['member_id']]);
            if ($member_figure_measure_data) {
                $member_figure_measure_data->save($params);
            } else {
                MemberFigureMeasureDataModel::create($params);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }

        return true;
    }
}